import React, { RefObject } from 'react'
import {
  Form,
  Input,
  Button,
} from 'antd-mobile'
import dayjs from 'dayjs'
import instance from '../../axios/instance'
import JSEncrypt from 'jsencrypt';
import {useNavigate} from 'react-router-dom'

export default () => {
  const navigate=useNavigate()
  const publicKey = `MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCuQd3gESx7VdIyRYUWjmjg61VIgUK6F45hClmqUMZ7xNiT5rlLM6e78osMvBF/yP7cVm7pK+NMCDWoVS1/AETpxJYqUlIC77ZAU8/MnP96IupnJL87vqhPcpdv7+VqLM38ls++yuD/F/HSoOQTv/leJh+dgE/4EYAJjOWFAbYfXwIDAQAB`;
  
    const encryptor = new JSEncrypt();
    encryptor.setPublicKey(publicKey);

  const encryptedData = (data) => {
    return encryptor.encrypt(data)
  }

  const onFinish = async(values) => {
    const encPwd = encryptedData(values.pwd);

    const res=await instance.post('/health/user/v1/login', {
      email: values.email,
      pwd: encPwd
    })
    console.log('res',res)
    if(res.status===200){
      localStorage.setItem('userName',res.data.result.userName)
      localStorage.setItem('headPic',res.data.result.headPic)
      navigate('/home')
    }
  }

  return (
    <div>
      <Form
        layout='horizontal'
        onFinish={onFinish}
      >
        <Form.Header>登录系统</Form.Header>
        <Form.Item
          name='email'
          label='邮箱'
          rules={[{ required: true, message: '邮箱不能为空' }]}
        >
          <Input placeholder='请输入姓名' />
        </Form.Item>

        <Form.Item
          name='pwd'
          label='密码'
          rules={[{ required: true, message: '密码不能为空' }]}
        >
          <Input placeholder='请输入姓名' />
        </Form.Item>

        <Form.Item>
          <Button block type='submit' color='primary' size='large'>
            提交
          </Button>
        </Form.Item>
      </Form>
    </div>
  )
}


